package com.doupi.partner.op.controller.configure;

import com.doupi.partner.dal.model.AdministratorPermission;
import com.doupi.partner.service.AdministratorPermissionService;
import com.doupi.partner.util.AjaxResponse;
import com.doupi.partner.util.DataTableData;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("configure/administratorPermission")
public class AdministratorPermissionController {
	private final static String TEMPLATE_FOLDER = "configure/";

	private final AdministratorPermissionService administratorPermissionService;

	@Autowired
	public AdministratorPermissionController(AdministratorPermissionService administratorPermissionService) {
		this.administratorPermissionService = administratorPermissionService;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index() {
		return TEMPLATE_FOLDER + "administratorPermission/index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<AdministratorPermission> list(Integer offset, Integer limit, AdministratorPermission administratorPermission) {
		Pagination<AdministratorPermission, AdministratorPermission> pagination = new Pagination<>(offset, limit, administratorPermission);
		pagination = administratorPermissionService.queryAdministratorPermissions(pagination);
		return new DataTableData<>(pagination.getTotalCount(), pagination.getRecords());
	}

	@ResponseBody
	@RequestMapping(value = "list/{parentId}", method = RequestMethod.GET)
	public AjaxResponse list(@PathVariable("parentId") Long parentId) {
		AjaxResponse ajaxResponse = AjaxResponse.defaultResponse();
		AdministratorPermission administratorPermission = new AdministratorPermission();
		administratorPermission.setParentId(parentId);
		List<AdministratorPermission> lists = administratorPermissionService.queryAdministratorPermissions(administratorPermission, new RowBounds(0, Integer.MAX_VALUE));
		ajaxResponse.setData(lists);
		return ajaxResponse;
	}

	@Transactional
	@ResponseBody
	@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
	public AjaxResponse delete(@PathVariable("id") Long id) {
		administratorPermissionService.deleteByPrimaryKey(id);
		return AjaxResponse.defaultResponse();
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(ModelMap modelMap, @PathVariable(value = "id") Long id) {
		AdministratorPermission administratorPermission = administratorPermissionService.selectByPrimaryKey(id);
		modelMap.put("administratorPermission", administratorPermission);
		return TEMPLATE_FOLDER + "administratorPermission/edit";
	}

	@RequestMapping(value = "addSubordinate/{parentId}", method = RequestMethod.GET)
	public String addSubordinate(ModelMap modelMap, @PathVariable(value = "parentId") Long parentId) {
		AdministratorPermission administratorPermission = new AdministratorPermission();
		AdministratorPermission administratorPermissionParent = administratorPermissionService.selectByPrimaryKey(parentId);
		administratorPermission.setParentId(parentId);
		if (null != administratorPermissionParent) {
			administratorPermission.setIdPath(administratorPermissionParent.getIdPath() + "/" + parentId);
		} else {
			administratorPermission.setIdPath("/" + parentId);
		}
		modelMap.put("administratorPermission", administratorPermission);
		return TEMPLATE_FOLDER + "administratorPermission/edit";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(AdministratorPermission administratorPermission) {
		if (null != administratorPermission.getParentId() && administratorPermission.getParentId() >= 0) {
			AdministratorPermission administratorPermissionParent = administratorPermissionService.selectByPrimaryKey(administratorPermission.getParentId());
			if (null != administratorPermissionParent) {
				administratorPermission.setIdPath(administratorPermissionParent.getIdPath() + "/" + administratorPermissionParent.getId());
			} else {
				administratorPermission.setIdPath("/0");
				administratorPermission.setParentId(0L);
			}
		} else {
			administratorPermission.setIdPath("/0");
			administratorPermission.setParentId(0L);
		}
		if (null == administratorPermission.getId() || administratorPermission.getId() <= 0) {
			administratorPermissionService.insert(administratorPermission);
		} else {
			administratorPermissionService.updateByPrimaryKeySelective(administratorPermission);
		}
		return "redirect:index";
	}
}
